package com.huangcy.gulimall.product.web;

import com.huangcy.gulimall.product.entity.CategoryEntity;
import com.huangcy.gulimall.product.service.CategoryService;
import com.huangcy.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @program: gulimall
 * @description:
 * @author: huangcy
 * @create: 2022-08-11 14:45
 **/
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redisTemplate;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        RLock lock = redisson.getLock("my-lock");
//        lock.lock();
        lock.lock(30, TimeUnit.SECONDS);
        try {
            System.out.println("加锁成功，执行业务。。。" + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {
        } finally {
            System.out.println("释放锁。。" + Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }


    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", categoryEntities);

        return "index";

    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @RequestMapping("/write")
    @ResponseBody
    public String writeValue() {
        String s = "";

        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = lock.writeLock();
        try {
            rLock.lock();
            System.out.println("读锁加锁成功！" + Thread.currentThread().getId());
            s = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("writeValue", s);
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("读锁释放成功！" + Thread.currentThread().getId());
            rLock.unlock();
        }
        return s;
    }


    @RequestMapping("/read")
    @ResponseBody
    public String readValue() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = lock.readLock();
        rLock.lock();
        String s = "";
        try {
            s = redisTemplate.opsForValue().get("writeValue");
            System.out.println("读锁加锁成功！" + Thread.currentThread().getId());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("读锁释放成功！" + Thread.currentThread().getId());
            rLock.unlock();
        }
        return s;
    }
}
